package com.fancy.mobileschool.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.AttendanceResult;
import com.fancy.mobileschool.service.AttendService;

import java.util.List;

public class AttendResultActivity extends AppCompatActivity {

    private String courseID;

    private String day;

    private AttendService attendService;

    private TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attend_result);
        resultText = (TextView) findViewById(R.id.result_text);
        attendService = new AttendService();

        Intent i = getIntent();
        courseID = i.getStringExtra("courseID");
        day = i.getStringExtra("day");
        GetStudentAttendResult task = new GetStudentAttendResult();
        String[] params = new String[2];
        params[0] = courseID;
        params[1] = day;

        task.execute(params);
    }

    private class GetStudentAttendResult extends AsyncTask<String, Void, List<AttendanceResult>> {

        @Override
        protected List<AttendanceResult> doInBackground(String... params) {
            String courseID = params[0];
            String day = params[1];
            return attendService.getStudentAttendsResult(courseID, day);
        }

        @Override
        protected void onPostExecute(List<AttendanceResult> attendanceResults) {
            if (attendanceResults != null && attendanceResults.size() > 0) {
                int size = attendanceResults.size();
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < size ; i ++) {
                    sb.append(attendanceResults.get(i).toString() + "\n");
                }

                resultText.setText(sb.toString());
            }
        }
    }
}
